package logic

import (
	"context"
	"order_srv/order"

	"lessson/internal/svc"
	"lessson/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddOrderLogic {
	return &AddOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddOrderLogic) AddOrder(req *types.Request) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	userId := 1
	addOrder, err := l.svcCtx.OrderSrv.AddOrder(l.ctx, &order.AddOrderRequest{
		UserId:  int64(userId),
		GoodsId: int64(req.GoodsId),
		Num:     int64(req.Num),
	})
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Code:    200,
		Message: "订单创建成功",
		Data: map[string]interface{}{
			"url": addOrder.Url,
		},
	}, nil
}
